/*******************************************************************************
 * Package: com.chris.product.controller
 * Type:    ProductController
 * Date:    2024/3/1 下午2:49
 *
 * Copyright (c) 2024 BOING CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.chris.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSON;
import com.chris.common.entity.Order;
import com.chris.common.entity.Product;
import com.chris.order.api.ProductFegin;
import com.chris.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO your comment
 *
 * @author Hudesheng
 * @date 2024/3/1 下午2:49
 */
@RestController
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    /*@Autowired
    private RestTemplate restTemplate;*/
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private ProductFegin productFegin;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Value("${spring.application.name}")
    private String appcationName;
    
    @GetMapping("/order/prod/{pid}")
    public Order addOrder(@PathVariable("pid") Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
        //第一种方式 直接写死
        //String url = "http://localhost:8081/product/";
        //ServiceInstance serviceInstance = discoveryClient.getInstances("service-product").get(0);
        //第二种方式 通过服务名获取其ip地址和端口号后进行拼接
        //url = serviceInstance.getHost()+":"+serviceInstance.getPort()+"/product/";
        //第三种方式，直接填服务名做负载均衡
        //url = "service-product/product/";
        //通过restTemplate调用商品微服务
        //Product product = restTemplate.getForObject("http://"+url + pid, Product.class);
        //第四种方式 通过fegin接口调用
        //Product product = productFegin.findByPid(pid);
        //第五种方式 通过dubbo接口调用
        Product product = productFegin.findByPid(pid);
        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
    
        //模拟网络延时
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Order order = new Order();
        if(product.getPid() == -1){
            order.setPname("下单失败");
            log.error("创建订单失败,订单信息为{}", JSON.toJSONString(order));
            return order;
        }
        
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        //orderService.save(order);
        log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));
        return order;
    }
    
    @RequestMapping("/order/message")
    @SentinelResource("message")
    public String message(String name,String age) {
        return "高并发下的问题测试,名称："+name+",年龄："+age+",服务名："+appcationName;
    }
    
    @RequestMapping("/order/message1")
    public String message1() {
        return "message1";
    }
    
    @RequestMapping("/order/message2")
    public String message2() {
        return "message2";
    }
}